<template>
	<view class="shzy-tree-select">
		<u-input type="select" :select-open="selectShow" v-model="innerValue" :placeholder="placeholder"
			@click="openSelect"></u-input>
		<cascader-tree v-if="selectShow" v-model="selectShow" @confirm="selectConfirm" :multiple="multiple"
			:props="props" :selectedData="selectedData" :options="options" :selectedId="!multiple ? [value] : value">
		</cascader-tree>
	</view>
</template>

<script>
	import cascaderTree from './cascader-tree';
	import {
		getProvinceSelectorInfoList
	} from '@/api/common.js'
	export default {
		model: {
			prop: 'value',
			event: 'input'
		},
		components: {
			cascaderTree
		},
		props: {
			value: {
				type: Array,
				default: () => []
			},
			placeholder: {
				type: String,
				default: '请选择'
			},
			options: {
				type: Array,
				default: () => []
			},
			props: {
				type: Object,
				default: () => ({
					label: 'fullName',
					value: 'id',
					children: 'children'
				})
			},
			disabled: {
				type: Boolean,
				default: false
			},
			multiple: {
				type: Boolean,
				default: false
			}
		},
		watch: {
			value(val) {
				if (val.length <= 0) return this.innerValue = ''
				this.setDefault(val)
			},
			options: {
				handler(val, oldVal) {
					this.allList = this.treeToArray(val)
					if (this.value.length > 0) {
						this.setDefault(this.value)
					}

				},
				deep: true
			}
		},
		data() {
			return {
				selectShow: false,
				innerValue: '',
				selectedData: [],
				allList: []
			}
		},
		methods: {
			treeToArray(options) {
				options = JSON.parse(JSON.stringify(options))
				let list = []
				const loop = (options) => {
					for (let i = 0; i < options.length; i++) {
						const item = options[i]
						list.push(item)
						if (item[this.props.children] && Array.isArray([this.props.children])) {
							loop(item[this.props.children])
						}
					}
				}
				loop(options)
				return list
			},
			setDefault(value) {
				if (!this.multiple) value = [value]
				let txt = []
				for (let i = 0; i < value.length; i++) {
					let val = JSON.parse(JSON.stringify(value[i]))
					for (let j = 0; j < val.length; j++) {
						inner: for (let k = 0; k < this.allList.length; k++) {
							if (val[j] === this.allList[k].id) {
								val[j] = this.allList[k].fullName;
								break;
							}
						}
					}
					txt.push(val)
				}
				this.selectedData = txt.map(o => o.join('-'))
				this.innerValue = this.selectedData.join(',')
			},

			openSelect() {
				if (this.disabled) return
				this.selectShow = true
			},
			selectConfirm(e, selectId) {
				this.selectedData = e;
				this.innerValue = e.join()
				if (this.multiple) {
					this.$emit('input', selectId)
					this.$nextTick(() => {
						this.$emit('changeFun', selectId)
					})
					return
				}
				this.$emit('input', selectId[0])
				this.$nextTick(() => {
					this.$emit('changeFun', selectId[0])
				})

			}
		}
	}
</script>
<style lang="scss" scoped>
	.shzy-tree-select {
		width: 100%;
	}
</style>
